package com.lanlian.commonlib.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.WindowManager;

import com.lanlian.commonlib.dao.DBManager;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.util.EventBusUtils;
import com.lanlian.uikit.LoadingDialog;
import com.lanlian.utillib.ToastUtils;
import com.lanlian.volleylib.HttpRequestManager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class BaseFragment extends Fragment {

    protected DBManager mDBManager;
    public LoadingDialog mLoadingDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDBManager = DBManager.getInstance();
        if (isRegisteredEventBus()) {
            EventBusUtils.register(this);
        }
    }

    /**
     * 初始化布局
     */
    protected void initView(View view) { }

    /**
     * 设置数据
     */
    protected void initData() { }

    /**
     * 设置监听
     */
    protected void setListeners() { }

    public void finish() {
        if (!getFragmentManager().popBackStackImmediate()) {
            getActivity().finish();
        }
    }

    public void showToast(String toast) {
        ToastUtils.show(getActivity(), toast);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        HttpRequestManager.getInstance().cancelAll(this.toString());
        if (isRegisteredEventBus()) {
            EventBusUtils.unregister(this);
        }
    }

    public void showLoadingDialog() {
        showLoadingDialog("加载中...");
    }

    /**
     * 显示loading对话框
     *
     * @param message 提示文字
     */
    public void showLoadingDialog(String message) {
        if (null == mLoadingDialog) {
            mLoadingDialog = new LoadingDialog(getActivity());
        }
        mLoadingDialog.setMessage(message);
        mLoadingDialog.setCancelable(true);
        mLoadingDialog.setCanceledOnTouchOutside(false);
        mLoadingDialog.show();
    }

    /**
     * loading对话框显示中
     */
    public boolean isLoadingDialogShowing() {
        return mLoadingDialog != null && mLoadingDialog.isShowing();
    }

    /**
     * loading对话框消失
     */
    public void dismissLoadingDialog() {
        try {
            if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
                getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 是否注册事件分发
     *
     * @return true 注册；false 不注册，默认不注册
     */
    protected boolean isRegisteredEventBus() {
        return false;
    }

    /**
     * 接收到分发的事件
     *
     * @param event 事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveEvent(EventMessage event) { }

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onReceiveStickyEvent(EventMessage event) { }

    private static final int MIN_DELAY_TIME= 2000;  // 两次点击间隔不能少于3000ms
    private long lastClickTime;

    public boolean isFastClick() {
        boolean flag = true;
        long currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - lastClickTime) >= MIN_DELAY_TIME) {
            flag = false;
            lastClickTime = currentClickTime;
        }
        return flag;
    }

}
